﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
namespace TuringCup
{
    /// <summary>
    /// AI控制指令
    /// </summary>
    [DataContract]
    public class AI_Commands
    {
        /// <summary>
        /// 表示新伸出触手的类
        /// </summary>
        [DataContract]
        public class NewTentacle
        {
            /// <summary>
            /// 起始Cell
            /// </summary>
            [DataMember]
            public int begin;
            /// <summary>
            /// 结束Cell
            /// </summary>
            [DataMember]
            public int end;
            public NewTentacle() { }
            public NewTentacle(int begin, int end)
            {
                this.begin = begin;
                this.end = end;
            }
        }

        /// <summary>
        /// 表示断开触手的类
        /// </summary>
        [DataContract]
        public class BreakTentacle
        {
            /// <summary>
            /// 起始Cell
            /// </summary>
            [DataMember]
            public int begin;
            /// <summary>
            /// 结束Cell
            /// </summary>
            [DataMember]
            public int end;
            /// <summary>
            /// 断开位置
            /// </summary>
            [DataMember]
            public double point;
            public BreakTentacle() { }
            public BreakTentacle(int begin,int end,Double point)
            {
                this.begin = begin;
                this.end = end;
                this.point = point;
            }
        }
        /// <summary>
        /// 新Tentacle 集合
        /// </summary>
        [DataMember]
        public List<NewTentacle> ntents;
        /// <summary>
        /// 断开Tentacle 集合
        /// </summary>
        [DataMember]
        public List<BreakTentacle> btents;
        public String Serialize()
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(AI_Commands));
            MemoryStream stream = new MemoryStream();
            ser.WriteObject(stream, this);
            Byte[] dataBytes = new Byte[stream.Length];
            stream.Position = 0;
            stream.Read(dataBytes, 0, (int)stream.Length);
            String dataString = Encoding.UTF8.GetString(dataBytes);
            return dataString;
        }
        /// <summary>
        /// 从JSON字符串中反序列化AI_Command
        /// </summary>
        /// <param name="json">JSON字符串</param>
        /// <returns></returns>
        public static AI_Commands Load(String json)
        {
            var mStream = new MemoryStream(Encoding.Default.GetBytes(json));
            return Load(mStream);
        }
        /// <summary>
        /// 从流对象中反序列化AI_Command
        /// </summary>
        /// <param name="json">JSON所在的流</param>
        /// <returns></returns>
        public static AI_Commands Load(Stream json)
        {
            AI_Commands commands;
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(AI_Commands));
            commands = (AI_Commands)ser.ReadObject(json);
            return commands;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        public AI_Commands()
        {
            ntents = new List<NewTentacle>();
            btents = new List<BreakTentacle>();
        }
    }
}
